package com.luoran.bartalks.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.luoran.bartalks.action.vo.UserMenuVo;
import com.luoran.bartalks.dao.IBtRoleMenuDao;
import com.luoran.bartalks.dao.IBtUserMenuDao;
import com.luoran.bartalks.dao.IMenuDao;
import com.luoran.bartalks.dao.IUserDao;
import com.luoran.bartalks.entity.BaseInfo;
import com.luoran.bartalks.entity.biz.BtRoleMenu;
import com.luoran.bartalks.entity.biz.BtUserMenu;
import com.luoran.bartalks.entity.biz.MenuInfo;
import com.luoran.bartalks.entity.biz.UserInfo;
import com.luoran.bartalks.service.IUserService;
import com.luoran.bartalks.utils.MD5;

@Service
public class UserService implements IUserService {

	@Autowired
	private IUserDao userDao;

	@Autowired
	private IMenuDao menuDao;
	
	@Autowired
	private IBtUserMenuDao userMenuDao;
	
	@Autowired
	private IBtRoleMenuDao roleMenuDao;

	public UserInfo getUser(String number, String pwd) {
		return userDao.userLogin(number, pwd);
	}

	/**
	 * 是否是有效的原始密码
	 * 
	 * @param number
	 *            账号
	 * @param pwd
	 *            未加密过的密码
	 * @return
	 */
	public boolean pwdSuccess(String number, String pwd) {
		UserInfo info = userDao.getInfoByNumber(number);
		if(info != null && !StringUtils.isEmpty(info.getLoginPwd())){
			return info.getLoginPwd().equalsIgnoreCase(MD5.get(pwd));
		}
		return false;
	}

	public void modifyPwd(String number, String newPwd) {
		userDao.modifyPwd(number, MD5.get(newPwd));
	}

	public UserInfo getInfoByNumber(String number) {
		return userDao.getInfoByNumber(number);
	}

	@Override
	public List<UserMenuVo> getUserMenus(String userId) {
		List<MenuInfo> list = menuDao.getUserMenus(userId);
		List<UserMenuVo> res = new ArrayList<UserMenuVo>();
		if (list.size() > 0) {
			for (MenuInfo menuInfo : list) {
				if (StringUtils.isEmpty(menuInfo.getParentId()) && menuInfo.getMenuStatus() == UserMenuVo.Enable_Status) {
					UserMenuVo menuVO = new UserMenuVo();
					menuVO.setName(menuInfo.getMenuName());
					menuVO.setNumber(menuInfo.getMenuNumber());
					menuVO.setUrl(menuInfo.getMenuUrl());
					menuVO.setIconClass(menuInfo.getMenuIcon());
					menuVO.getChilds().addAll(findChilds(menuInfo.getId(), list));
					res.add(menuVO);
				}
			}
		}
		// 按菜单编码的自然顺序进行排序
		Collections.sort(res, new Comparator<UserMenuVo>() {
			public int compare(UserMenuVo o1, UserMenuVo o2) {
				return o1.getNumber().compareTo(o2.getNumber());
			}
		});
		return res;
	}

	/**
	 * 寻找菜单的子菜单集合
	 * 
	 * @param id
	 * @param list
	 * @return
	 */
	List<UserMenuVo> findChilds(String id, List<MenuInfo> list) {
		List<UserMenuVo> res = new ArrayList<UserMenuVo>();
		for (MenuInfo menuInfo : list) {
			if (id.equals(menuInfo.getParentId()) && menuInfo.getMenuStatus() == UserMenuVo.Enable_Status) {
				UserMenuVo menuVO = new UserMenuVo();
				menuVO.setName(menuInfo.getMenuName());
				menuVO.setNumber(menuInfo.getMenuNumber());
				menuVO.setUrl(menuInfo.getMenuUrl());
				menuVO.setIconClass(menuInfo.getMenuIcon());
				res.add(menuVO);
			}
		}
		// 按菜单编码的自然顺序进行排序
		Collections.sort(res, new Comparator<UserMenuVo>() {
			public int compare(UserMenuVo o1, UserMenuVo o2) {
				return o1.getNumber().compareTo(o2.getNumber());
			}
		});
		return res;
	}

	public UserInfo getUser(String id) {
		return userDao.getInfo(id);
	}

	//分页
	@Override
	public PageQuery<? extends BaseInfo> getQueryList(PageQuery<? extends BaseInfo> pageQuery) {
		userDao.queryPage(pageQuery);
		return pageQuery;
	}

	//根据主键查询
	@Override
	public UserInfo getUserInfo(String id) {
		 
		return userDao.single(id);
	}

	//新增用户
	@Override
	public String addUser(UserInfo info) {
		userDao.insertTemplate(info);
		return info.getId();
		
	}

	//根据主键更新
	@Override
	public int editUserInfo(UserInfo info) {
		return userDao.updateTemplateById(info);
		
	}

	//逻辑删除
	@Override
	public int updateStatus(UserInfo info) {
		
		return userDao.updateTemplateById(info);
	}

	//获取菜单树
	@Override
	public List<MenuInfo> getMenuTree() {
		
		return menuDao.all();
	}

	//操作用户菜单
	@Override
	public String addMenuForUser(BtUserMenu userMenu) {
		userMenuDao.insertTemplate(userMenu);
		return userMenu.getId();
		
	}

	//获取菜单树中生效的用户菜单
	@Override
	public List<BtUserMenu> getUserMenu(String userId) {
		return userMenuDao.getUserMenu(userId);
	}

	//删除用户原有菜单
	@Override
	public void deleteUserMenus(String userId) {
		userMenuDao.deleteUserMenus(userId);
		
	}

	//校验用户名是否被占用
	@Override
	public UserInfo checkUserName(String loginNo) {

		return userDao.selectUserByLoginNo(loginNo);
	}

	//邮箱校验
	@Override
	public UserInfo checkEmail(String email) {
		
		return userDao.selectUserByEmail(email);
	}

	//获取角色的通用菜单
	@Override
	public List<BtRoleMenu> getMenuByRole(String role) {
		
		return roleMenuDao.getRoleMenu(role);
	}

	//根据当前登录账号查询公司成员
	@Override
	public List<UserInfo> getProjectMembers(String companyId) {
		
		return userDao.getProjectMembers(companyId);
	}

}
